package com.info.factory;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.ability.AbilityEnum;
import com.info.SkillEnum;
import com.info.SkillInfo;
import com.info.SpecializationSkillInfo;
import com.info.factory.db.SkillFactoryDbHelper;

public class SkillFactory {
	
	private static SkillFactory INSTANCE;
	
	private static final int NAME_COLUMN = 0;
	private static final int DESCRIPTION_COLUMN = 1;
	private static final int TRAINING_COLUMN = 2;
	private static final int ARMOUR_COLUMN = 3;
	private static final int ABILITY_COLUMN = 4;
	
	
	private SkillFactoryDbHelper helper;
	
	private String nameColumn[] = {"Name"};
	
	private SkillFactory(Context context){
		helper = new SkillFactoryDbHelper(context);
		
	}
	
	public static SkillFactory getInstance(Context context){
		if (INSTANCE == null){
			INSTANCE = new SkillFactory(context);
		}
		return INSTANCE;
	}
	
	public SkillInfo getSkill(SkillEnum skillEnum){
		if (!skillEnum.isSpecial()){
			return buildSkill(skillEnum, new SkillInfo());
		}
		return null;
	}
	
	private SkillInfo buildSkill(SkillEnum skillEnum, SkillInfo skill){
		SQLiteDatabase database = helper.getReadableDatabase();
		Cursor c = database.query(SkillFactoryDbHelper.TABLE_NAME, nameColumn, "Name = " + skillEnum.getName(), null, null, null, null);
		if (c.getCount() != 1){
			c.close();
			return null;
		}
		skill.setName(c.getString(NAME_COLUMN));
		skill.setDescription(c.getString(DESCRIPTION_COLUMN));
		skill.setRequiresTraining(Boolean.parseBoolean(c.getString(TRAINING_COLUMN)));
		skill.setArmourApplied(Boolean.parseBoolean(c.getString(ARMOUR_COLUMN)));
		skill.setAbilityType(AbilityEnum.valueOf(c.getString(ABILITY_COLUMN)));
		c.close();
		database.close();
		return skill;
	}
	
	public SkillInfo getSpecializedSkill(SkillEnum skillEnum, String specialization){
		if (skillEnum.isSpecial()){
			SpecializationSkillInfo info = (SpecializationSkillInfo) buildSkill(skillEnum, new SpecializationSkillInfo());
			info.setSpecification(specialization);
			return info;
		}
		return null;
	}
}
